package com.csxy.hql.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csxy.hql.R;
import com.csxy.hql.entity.EduCourse;
import com.csxy.hql.entity.vo.*;
import com.csxy.hql.service.EduCourseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {
    @Autowired
    private EduCourseService eduCourseService;

    //添加课程基本信息方法
    @RequestMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoForm courseInfoForm) {
        //根据前端页面做的一个表单实体类，用于接收前端传回来的对象
        String id = eduCourseService.addCourseInfo(courseInfoForm);
        //回传ID主要是下一步创建章节时候，把章节和课程关联起来
        return R.ok().data("courseId", id);
    }

    //查询课程信息
    @GetMapping("/getCourseInfoById/{courseId}")
    public R getCourseInfoById(@PathVariable String courseId) {
        CourseInfoForm courseInfoForm = eduCourseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoForm", courseInfoForm);
    }

    //查询课程信息列表
    @GetMapping("/getCourseList/")
    public R getCourseList() {
        List<EduCourse> list = eduCourseService.list(null);
        return R.ok().data("list", list);
    }

    //修改课程信息
    @PostMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoForm courseInfoForm) {
        eduCourseService.updateCourseInfo(courseInfoForm);
        return R.ok();
    }


    //课程列表中删除课程方法
    @DeleteMapping("/removeCourseById/{id}")
    public R removeCourseById(@PathVariable String id) {
        boolean flag = eduCourseService.removeCourse(id);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //多条件分页查询
    @Operation(description = "多条件查询课程带分页")
    @PostMapping("/pageCourseCondition/{page}/{limit}")
    public R pageCourseCondition(@Parameter(name = "page", description = "当前页码", required = true)
                                 @PathVariable Long page,
                                 @Parameter(name = "limit", description = "每页记录数", required = true)
                                 @PathVariable Long limit,
                                 @RequestBody(required = false) CourseQuery courseQuery){ //通过封装的CourseQuery对象来直接传入条件
        //创建分页page对象
        Page<EduCourse> pageParm = new Page<>(page,limit);
        //调用方法实现条件查询分页
        eduCourseService.pageQuery(pageParm,courseQuery);
        //获取查询结果数据
        List<EduCourse> records = pageParm.getRecords();
        //获取总记录数
        long total = pageParm.getTotal();
        return R.ok().data("total",total).data("rows",records);
    }

    //根据课程id查询课程确认信息
    @GetMapping("/getpublishCourseInfo/{id}")
    public R getpublishCourseInfo(@PathVariable String id) {
        CoursePublishVo publishCourseInfo = eduCourseService.getPublishCourseInfo(id);
        return R.ok().data("publishCourse", publishCourseInfo);
    }

    //根据课程id,查询课程信息【订单】
    @PostMapping("/getCourseInfoByIdOrder/{courseId}")
    public EduCourseVo getCourseInfoByIdOrder(@PathVariable String courseId) {
        CourseWebVo courseInfo = eduCourseService.getBaseCourseInfo(courseId);
        EduCourseVo eduCourseVo = new EduCourseVo();
        BeanUtils.copyProperties(courseInfo,eduCourseVo);
        return eduCourseVo;
    }

    //发布课程信息
    @PostMapping("/publishCourse/{id}")
    public R publishCourse(@PathVariable String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        eduCourse.setGmtCreate(new Date());
        eduCourse.setGmtModified(new Date());
        boolean flag = eduCourseService.updateById(eduCourse);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}
